<?php

namespace app\admin\controller;


class Upload extends Base
{


	//商品图片上传
	public function uploadProductsImg()
	{
		return $this->uploadImg("products");
	}

    //产品分类上传图片
    public function uploadCateImg()
    {
        return $this->uploadImg("cate");
    }



    // 上传单张缩略图
    private function uploadImg($dirName = "")
    {
        if(request()->isAjax()){
            $file = request()->file('file');
            $filePath = '/upload' . '/' . date('Ymd') . '/';
            $dirPath = ROOT_PATH . 'public' . DS . 'upload';
            if($dirName){
                $filePath = '/upload' . '/' .$dirName .'/' . date('Ymd') . '/';
                $dirPath = ROOT_PATH . 'public' . DS . 'upload/'.$dirName;
            }
            //判断目录费否存在
            if(!is_dir($dirPath)){
                mkdir($dirPath);
            }
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move($dirPath);
            if($info){
                $src =  'http://'.$_SERVER['HTTP_HOST'].$filePath . $info->getFilename();
                return json(msg(0, ['src' => $src], ''));
            }else{
                // 上传失败获取错误信息
                return json(msg(-1, '', $file->getError()));
            }
        }
    }

}